#include "SDLWindow.h"

SDLWindow::SDLWindow(string title, int width, int height)
{
    window = nullptr;
    renderer = nullptr;
    isExit = false;
    frameRate = 30;
    startFrameTime = 0;
    endFrameTime = 0;
    frameCount = 0;
    frameRateStartTime = 0;

    window_width = width;
    window_height = height;
    SDLTool::SDLInit();

    window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, 
        SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
    if (window == nullptr)
    {
        cout << SDL_GetError() << endl;
        return;
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == nullptr)
    {
        cout << SDL_GetError() << endl;
        return;
    }
}

SDLWindow::~SDLWindow()
{
    if( window != nullptr )
    {
        SDL_DestroyWindow(window);
    }

    if( renderer != nullptr )
    {
        SDL_DestroyRenderer(renderer);
    }


}

/**
 * @brief 将背景绘制到窗口上
 * 
 */
void SDLWindow::drawBackground()
{
    SDL_Rect dstPos;
//      SDL_QueryTexture(background, NULL, NULL, &pos.w, &pos.h);
    dstPos.x = 0;
    dstPos.y = 0;
    dstPos.w = window_width;
    dstPos.h = window_height;
    SDL_RenderCopy(renderer, background, NULL, &dstPos);
}

/**
 * @brief 清空图像
 * 
 */
void SDLWindow::clear()
{
    SDL_RenderClear(renderer);
}


/**
 * @brief 显示图像
 * 
 */
void SDLWindow::show()
{
    if( background != nullptr )
    {
        drawBackground();
    }

    for( unsigned int i = 0; i < sdlBaseList.size(); i++ )
    {
        sdlBaseList[i]->show(renderer);
    }
    SDL_RenderPresent(renderer);


    endFrameTime = SDL_GetTicks();
    if( (endFrameTime-startFrameTime) < (1000 / frameRate) )
    {
        // 休眠一段时间，时长为当前帧的剩余时间。
        SDL_Delay( ( 1000 / frameRate ) - (endFrameTime-startFrameTime) );
    }

    //更新时间
    startFrameTime = SDL_GetTicks();

    if( frameCount%30 == 29 )
    {
        cout << "FPS:" << 30000.0/(endFrameTime-frameRateStartTime) << endl;
        frameRateStartTime = endFrameTime;
    }
    frameCount++;

}


/**
 * @brief 获取window
 * 
 * @return SDL_Window* : 此window使用的window
 */
SDL_Window *SDLWindow::getSDLWindow()
{
    return this->window;
}


/**
 * @brief 获取Render
 * 
 * @return SDL_Renderer* : 此window使用的Render
 */
SDL_Renderer *SDLWindow::getSDLRender()
{
    return this->renderer;
}


/**
 * @brief 添加控件到队列中
 * 
 */
void SDLWindow::addSDLBase(SDLBase *sdlBase)
{
    sdlBaseList.push_back(sdlBase);
}


/**
 * @brief 处理按键
 * 
 * @param event ： 传递过来的事件
 */
void SDLWindow::handleEvent(SDL_Event event)
{
    //如果用户点击了窗口右上角的关闭按钮
    switch( event.type )
    {
        case SDL_QUIT:
            //退出程序
            isExit = true;
        break;

        case SDL_WINDOWEVENT:
            if (SDL_WINDOWEVENT_CLOSE == event.window.event) 
            {
                isExit = true;
            }
        break;
        default: 
        break;
    }
    for( unsigned int i = 0; i < sdlBaseList.size(); i++ )
    {
        sdlBaseList[i]->handleEvent(event);
    }
}


/**
 * @brief 设置窗口位置
 * 
 * @param x ： 窗口x位置
 * @param y ： 窗口y位置
 */
void SDLWindow::setWindowPos(int x, int y)
{
    SDL_SetWindowPosition(window, x, y);
}



/**
 * @brief 设置背景
 * 
 * @param filename ： 需要设置的背景图片
 */
void SDLWindow::setBackground(string filename)
{
    this->background = LoadImage( renderer, filename);
}
